package com.yc.spring_boot_04.config;

import com.yc.spring_boot_04.component.LoginHandlerInterceptor;
import com.yc.spring_boot_04.component.MyLocaleResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class MyMvcConf {

    @Bean // 配置国际化
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }

    @Bean // 配置视图跳转 也可以写controller
    public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {
        WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
            }

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                // TODO: springboot 2.x 之后要排除静态资源
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/index.html", "/", "/user/login", "/webjars/**", "/css/**", "/img" +
                                "/**", "/js/**");
            }
        };

        return adapter;
    }

}
